package com.gxa.p2p.controller;

import com.gxa.p2p.common.JsonResult;
import com.gxa.p2p.pojo.Borrowuser;
import com.gxa.p2p.service.BorrowuserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/borrow")

//@Transactional
@Api(value = "借款用户操作接口")
public class BorrowuserController {
    @Autowired
    BorrowuserService borrowuserService;
    /**
     * 周志远
     * @param borrowuser
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增借款用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "borrowuser",value = "新增的借款用户",required = true,dataType = "Borrowuser")
    })
    public Map addBorrower (Borrowuser borrowuser){
        Boolean add = borrowuserService.add(borrowuser);
        JsonResult jsonResult = new JsonResult();
        if (add){

            jsonResult.setCode("200");
            return jsonResult.getDataMap();
        }
        jsonResult.setCode("0");
        return jsonResult.getDataMap();
    }
    /**
     * 周志远
     * @param borrowuser
     * @return
     */
    @PostMapping("/findBorrowerByPage")
    @ApiOperation("查询借款用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyName",value = "借款用户姓名",required = false,dataType = "String"),
            @ApiImplicitParam(name = "phone",value = "借款用户手机",required = false,dataType = "String"),
            @ApiImplicitParam(name = "lockState",value = "借款用户状态",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "每页显示数",required = true,dataType = "Integer")

    })
    public Map findBorrowerByPage (String companyName, String phone, Boolean lockState, Integer page,Integer size){

        List<Map<String, Object>> byPage = borrowuserService.findByPage(companyName, phone, lockState, page, size);
        List<Map<String, Object>> count = borrowuserService.count(companyName, phone, lockState, null, null);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(byPage);
        jsonResult.put("count",count.size());
        return jsonResult.getDataMap();
    }
    /**
     * 周志远
     * @param
     * @return
     */
    @PostMapping("/borrowerDetail")
    @ApiOperation("查看借款用户详细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "借款用户编号",required = true,dataType = "Long")


    })
    public Map borrowerDetail (Long id){
        Borrowuser borrowuser = borrowuserService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(borrowuser);
        return jsonResult.getDataMap();
    }
    /**
     * 周志远
     * @param
     * @return
     */
    @PostMapping("/BorrowuserPwd")
    @ApiOperation("借款用户密码管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "投资用户编号",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "loginPwd",value = "投资用户登录密码",required = true,dataType = "String"),
            @ApiImplicitParam(name = "dealPwd",value = "投资用户交易密码",required = true,dataType = "String")
    })
    public Map borrowuserPwd (Long id,String loginPwd,String dealPwd){
        borrowuserService.updatePwd(id,loginPwd,dealPwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /**
     * 周志远
     * @param investuser
     * @return
     */
    @PostMapping("/updateBorrowuser")
    @ApiOperation("借款用户资料修改 (测试只传注释中修改开头的字段)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "修改的借款用户的编号(不变)",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "phone",value = "修改的借款用户的手机号",required = true,dataType = "String"),
            @ApiImplicitParam(name = "userType",value = "修改的借款用户的类型",required = true,dataType = "String"),
            @ApiImplicitParam(name = "companyName",value = "修改的借款用户的企业名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "taxNumber",value = "修改的借款用户的税务登记证号",required = true,dataType = "String"),
            @ApiImplicitParam(name = "legalName",value = "修改的借款用户的法人姓名",required = true,dataType = "String"),
            @ApiImplicitParam(name = "email",value = "修改的借款用户的邮箱",required = true,dataType = "String"),
            @ApiImplicitParam(name = "lockState",value = "修改的借款用户的状态",required = true,dataType = "Boolean"),
            @ApiImplicitParam(name = "remarks",value = "修改的借款用户的备注",required = true,dataType = "String")

    })
    public Map updateInvestor (Borrowuser borrowuser){
        System.out.println("111111111111111"+borrowuser);
        borrowuserService.updateBorrowuser(borrowuser);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();

    }

    /**
     * 周志远
     * @param
     * @return
     */
    @PostMapping("/findByName")
    @ApiOperation("新增借款模块选择借款方")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyName",value = "要搜索的名称（可模糊查询）",required = false,dataType = "String")

    })
    public Map findByName (String companyName){
        System.out.println(companyName);
        List<Map<String, Object>> byName = borrowuserService.findByName(companyName);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(byName);
        return jsonResult.getDataMap();

    }

}
